home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Graphics Unleashed
/
PC Graphics Unleashed.iso
/
ch09
/
draw.c
next >
Wrap
C/C++ Source or Header
|
1994-09-15
|
5KB
|
235 lines
// DRAW.C
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
#define TOL 0.0005
#define PI 3.14159265358979323846
typedef struct {
float x,y,z;
} POINT3D;
typedef struct {
int x,y;
} POINTint;
/* World Limits in 2D */
float WXleft;
float WXright;
float WYtop;
float WYbottom;
/* Device Limits */
int DXmin;
int DYmin;
int DYmax;
int DXmax;
/* Rotations for 3D View in Radians*/
float RX;
float RY;
float RZ;
float COSRX;
float SINRX;
float COSRY;
float SINRY;
float COSRZ;
float SINRZ;
void InitGraphics(void);
float DegToRad(float deg);
float RadToDeg(float rad);
POINT3D World3DToWorld2D(POINT3D p);
POINTint World2DToDevice(POINT3D p);
void drawpoint(POINT3D p1);
void drawline(POINT3D p1,POINT3D p2);
void drawaxis(void);
void SetAxesAngles(float rx, float ry, float rz);
void main()
{
POINT3D p;
InitGraphics();
/* World Limits */
WXleft = -1.2;
WXright = 1.2;
WYtop = 1.2;
WYbottom = -1.2;
/* Device Limits On A Viewport */
DXmin = 0.0;
DYmin = 0.0;
DYmax = getmaxy()/2;
DXmax = getmaxy()/2;
/* Draw A 2X2 Grid */
setcolor(WHITE);
rectangle(0,0,getmaxy(),getmaxy());
line(getmaxy()/2,0,getmaxy()/2,getmaxy());
line(0,getmaxy()/2,getmaxy(),getmaxy()/2);
/* Initialize a 3D point */
p.x = 0.5;
p.y = 0.2;
p.z = 0.5;
/* TOP VIEW */
setviewport(0,0,getmaxy()/2,getmaxy()/2,1);
SetAxesAngles(90, 0, 0);
drawaxis();
drawpoint(p);
/* ISO VIEW */
setviewport(getmaxy()/2,0,getmaxy(),getmaxy()/2,1);
SetAxesAngles(20, -15, -5);
drawaxis();
drawpoint(p);
/* FRONT VIEW */
setviewport(0,getmaxy()/2,getmaxy()/2,getmaxy(),1);
SetAxesAngles(0, 0, 0);
drawaxis();
drawpoint(p);
/* SIDE VIEW */
setviewport(getmaxy()/2,getmaxy()/2,getmaxy(),getmaxy(),1);
SetAxesAngles(0, -90, 0);
drawaxis();
drawpoint(p);
getch();
closegraph();
}
/* /////////////////////////////////////////////////////// */
void InitGraphics(void)
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
if (gdriver != VGA) {
printf("VGA graphics card required.\n");
exit(1);
}
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
setviewport(0,0,getmaxx(),getmaxy(),1);
}
float DegToRad(float deg)
{
return(deg*PI/180.0);
}
float RadToDeg(float rad)
{
return(rad*180.0/PI);
}
void SetAxesAngles(float rx, float ry, float rz) {
RX = DegToRad(rx);
RY = DegToRad(ry);
RZ = DegToRad(rz);
COSRX = cos(RX);
SINRX = sin(RX);
COSRY = cos(RY);
SINRY = sin(RY);
COSRZ = cos(RZ);
SINRZ = sin(RZ);
}
POINT3D World3DToWorld2D(POINT3D p)
{
POINT3D ptemp;
ptemp = p;
if (RX) {
ptemp.x = p.x;
ptemp.y = COSRX*p.y - SINRX*p.z;
ptemp.z = SINRX*p.y + COSRX*p.z;
p = ptemp;
}
if (RY) {
ptemp.x = COSRY*p.x + SINRY*p.z;
ptemp.y = p.y;
ptemp.z = -SINRY*p.x + COSRY*p.z;
p = ptemp;
}
if (RZ) {
ptemp.x = COSRZ*p.x - SINRZ*p.y;
ptemp.y = SINRZ*p.x + COSRZ*p.y;
ptemp.z = p.z;
}
if (fabs(ptemp.x) < TOL) ptemp.x = 0.0;
if (fabs(ptemp.y) < TOL) ptemp.y = 0.0;
if (fabs(ptemp.z) < TOL) ptemp.z = 0.0;
return(ptemp);
}
POINTint World2DToDevice(POINT3D p)
{
POINTint ptemp;
ptemp.x = (WXleft-p.x)*(DXmax-DXmin)/(WXleft-WXright) + DXmin + 0.5;
ptemp.y = (WYtop-p.y)*(DYmax-DYmin)/(WYtop-WYbottom) + DYmin + 0.5;
return(ptemp);
}
void drawpoint(POINT3D p1)
{
/* draws a 3D point */
POINTint p2;
p1.z = -p1.z;
p2 = World2DToDevice(World3DToWorld2D(p1));
//circle(p2.x,p2.y,2);
rectangle(p2.x - 2, p2.y - 2, p2.x + 2, p2.y + 2);
}
void drawline(POINT3D p1,POINT3D p2)
{
/* draws a 3D line */
POINTint p11,p22;
p1.z = -p1.z;
p2.z = -p2.z;
p11 = World2DToDevice(World3DToWorld2D(p1));
p22 = World2DToDevice(World3DToWorld2D(p2));
line(p11.x,p11.y,p22.x,p22.y);
}
void drawaxis(void) {
POINT3D p1,p2;
p1.x = p1.y = p1.z = 0;
p2.x = 1; p2.y = 0; p2.z=0;
setcolor(RED);
drawline(p1,p2);
p2.x = 0; p2.y = 1; p2.z=0;
setcolor(GREEN);
drawline(p1,p2);
p2.x = 0; p2.y = 0; p2.z=1;
setcolor(BLUE);
drawline(p1,p2);
setcolor(WHITE);
}